<script setup lang="ts">
import { ref } from "vue";
import ReCol from "@/components/ReCol";
import { formRules } from "./utils/rule";
import { FormProps } from "./utils/types";
import { transformI18n } from "@/plugins/i18n";
import { IconSelect } from "@/components/ReIcon";
import Segmented from "@/components/ReSegmented";
import ReAnimateSelector from "@/components/ReAnimateSelector";
import {
  menuTypeOptions,
  showLinkOptions,
  fixedTagOptions,
  keepAliveOptions,
  hiddenTagOptions,
  showParentOptions,
  frameLoadingOptions,
  showIfremOptions,
  showIfremTypeOptions
} from "./utils/enums";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({
    menuType: "",
    higherMenuOptions: [],
    parentId: 0,
    menuName: "",
    pathName: "",
    path: "",
    component: "",
    orderNum: 1,
    icon: "",
    isFrame: "",
    targetType: "",
    isCache: "",
    visible: "",
    status: "",
    perms: "",
    query: ""
  })
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);
console.log("newFormInline", newFormInline.value);

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="newFormInline.menuType">
            <el-radio :label="'M'">目录</el-radio>
            <el-radio :label="'C'">菜单</el-radio>
            <el-radio :label="'A'">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
      </re-col>

      <re-col>
        <el-form-item label="上级菜单" prop="parentId">
          <el-cascader
            v-model="newFormInline.parentId"
            class="w-full"
            :options="newFormInline.higherMenuOptions"
            :props="{
              value: 'menuId',
              label: 'menuName',
              emitPath: false,
              checkStrictly: true
            }"
            clearable
            filterable
            placeholder="请选择上级菜单"
          >
            <template #default="{ node, data }">
              <span>{{ transformI18n(data.menuName) }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input
            v-model="newFormInline.menuName"
            clearable
            placeholder="请输入菜单名称"
          />
        </el-form-item>
      </re-col>
      <re-col
        v-if="newFormInline.menuType !== 'A'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="路由名称" prop="pathName">
          <el-input
            v-model="newFormInline.pathName"
            clearable
            placeholder="请输入路由名称"
          />
        </el-form-item>
      </re-col>

      <re-col
        v-if="newFormInline.menuType !== 'A'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="路由路径" prop="path">
          <el-input
            v-model="newFormInline.path"
            clearable
            placeholder="请输入路由路径"
          />
        </el-form-item>
      </re-col>
      <re-col
        v-show="newFormInline.menuType === 'C'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="组件路径" prop="component">
          <el-input
            v-model="newFormInline.component"
            clearable
            placeholder="请输入组件路径"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="菜单排序" prop="orderNum">
          <el-input-number
            v-model="newFormInline.orderNum"
            class="!w-full"
            :min="1"
            :max="9999"
            controls-position="right"
          />
        </el-form-item>
      </re-col>

      <re-col
        v-show="newFormInline.menuType !== 'A'"
        :value="12"
        :xs="24"
        :sm="24"
      >
        <el-form-item label="菜单图标" prop="icon">
          <IconSelect v-model="newFormInline.icon" class="w-full" />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <!-- 按钮级别权限设置 -->
        <el-form-item label="权限标识" prop="perms">
          <el-input
            v-model="newFormInline.perms"
            clearable
            placeholder="请输入权限标识"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="菜单显示" prop="visible">
          <Segmented
            :modelValue="newFormInline.visible"
            :options="showLinkOptions"
            @change="
              ({ option: { value } }) => {
                newFormInline.visible = value;
              }
            "
          />
        </el-form-item>
      </re-col>
      <re-col
        :value="12"
        :xs="24"
        :sm="24"
        v-show="newFormInline.menuType !== 'A'"
      >
        <el-form-item label="是否外链" prop="isFrame">
          <Segmented
            :modelValue="newFormInline.isFrame"
            :options="showIfremOptions"
            @change="
              ({ option: { value } }) => {
                newFormInline.isFrame = value;
              }
            "
          />
        </el-form-item>
      </re-col>

      <re-col
        :value="12"
        :xs="24"
        :sm="24"
        v-show="newFormInline.menuType !== 'A' && newFormInline.isFrame == '0'"
      >
        <el-form-item label="打开方式" prop="targetType">
          <Segmented
            :modelValue="newFormInline.targetType"
            :options="showIfremTypeOptions"
            @change="
              ({ option: { value } }) => {
                newFormInline.targetType = value;
              }
            "
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="缓存页面" prop="isCache">
          <Segmented
            :modelValue="newFormInline.isCache"
            :options="keepAliveOptions"
            @change="
              ({ option: { value } }) => {
                newFormInline.isCache = value;
              }
            "
          />
        </el-form-item>
      </re-col>
    </el-row>
  </el-form>
</template>
